<template>
<!--    -->
<div class="recommend" v-loading:[loadingText]="loading">
  <scroll class="recommend-content">
    <div>
      <div class="slider-wrapper">
        <div class="slider-content">
          <!-- 当sliders没有数据的时候就不会渲染，有了之后才会渲染  -->
          <slider v-if="sliders.length" :sliders="sliders"></slider>
        </div>
      </div>
      <div class="recommend-list">
        <h1 class="list-title" v-show="!loading">热门歌单推荐</h1>
        <ul>
          <li
            v-for="item in albums"
            class="item"
            :key="item.id"
          >
            <div class="icon">
              <img v-lazy="item.pic" width="60" height="60">
            </div>
            <div class="text">
              <h2 class="name">{{ item.username }}</h2>
              <p class="title">{{ item.title }}</p>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </scroll>
</div>
</template>

<script type="text/ecmascript-6">
import { getRecommend } from '@/service/recommend'
import Slider from '@/components/base/slider/slider'
import Scroll from '@/components/base/scroll/scroll'

/*
* options-api优势：结构清晰，对于逻辑比较少的组件，看起来清晰，是一个描述性结构，很直观
* composition-api: 适用于逻辑复杂，可重新使用的
* 开发组件时要按照实际情况来
* */

export default {
  name: 'recommend',
  components: {
    Slider,
    Scroll
  },
  data() {
    return {
      sliders: [],
      // 歌单推荐列表
      albums: [],
      loadingText: '加载中，请稍后...'
    }
  },
  computed: {
    loading() {
      // 当没有轮播和推荐列表的时候返回true
      // 当数据都有了之后返回false 删除加载层
      return !this.sliders.length && !this.albums.length
    }
  },
  // 因为是异步获取的，将原本的回调方式改为async+await让异步同步化
  async created() {
    const result = await getRecommend()
    this.sliders = result.sliders
    this.albums = result.albums
  }
}
</script>

<style lang="scss" scoped>
  .recommend {
    position: fixed;
    width: 100%;
    top: 88px;
    bottom: 0;
    overflow: scroll;

    .recommend-content {
      /* BS外城必须比内容层要矮才可以滚动 */
      height: 100%;
      overflow: hidden;
      .slider-wrapper {
        position: relative;
        width: 100%;
        height: 0;
        padding-top: 40%;
        overflow: hidden;
        .slider-content {
          position: absolute;
          left: 0;
          top: 0;
          width: 100%;
          height: 100%;
        }
      }
      .recommend-list {
        .list-title {
          height: 65px;
          line-height: 65px;
          text-align: center;
          font-size: $font-size-medium;
          color: $color-theme;
        }
        .item {
          display: flex;
          box-sizing: border-box;
          align-items: center;
          padding: 0 20px 20px 20px;

          .icon {
            flex: 0 0 60px;
            width: 60px;
            padding-right: 20px;
          }
          .text {
            display: flex;
            flex-direction: column;
            justify-content: center;
            flex: 1;
            line-height: 20px;
            overflow: hidden;
            font-size: $font-size-medium;
          }
          .name {
            margin-bottom: 10px;
            color: $color-text;
          }
          .title {
            color: $color-text-d;
          }
        }
      }
    }
  }
</style>
